package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.rules.*;
    import mortal.game.view.runBusiness.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class RunBusinessController extends Controller
    {
        private var _runBusinessModule:RunBusinessModule;
        private var _runBusinessSilverNoteModule:RunBusinessSilverNoteModule;
        private var _npcId:int = -1;
        private var _introWindow:RunBusinessIntroWindow;

        public function RunBusinessController()
        {
            
        }

        public function get runBusinessModule() : RunBusinessModule
        {
            if (this._runBusinessModule == null)
            {
                this._runBusinessModule = new RunBusinessModule();
            }
            return this._runBusinessModule;
        }

        public function get runBusinessSilverNoteModule() : RunBusinessSilverNoteModule
        {
            if (this._runBusinessSilverNoteModule == null)
            {
                this._runBusinessSilverNoteModule = new RunBusinessSilverNoteModule();
            }
            return this._runBusinessSilverNoteModule;
        }

        override protected function initView() : IView
        {
            this._runBusinessModule = new RunBusinessModule();
            return this._runBusinessModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.UseTaskSilverNote, this.onUseTaskSilverNoteHandler);
            Dispatcher.addEventListener(EventName.RunBusinessTaskAccept, this.onRunBusinessTaskAcceptHandler);
            Dispatcher.addEventListener(EventName.RunBusinessCurrentNpc, this.onRunBusinessCurrentNpcHandler);
            Dispatcher.addEventListener(EventName.RunBusinessSellGoods, this.onRunBusinessSellGoodsHandler);
            Dispatcher.addEventListener(EventName.RunBusinessBuyGoods, this.onRunBusinessBuyGoodsHandler);
            Dispatcher.addEventListener(EventName.RunBusinessGetGoodsSuccess, this.onGetGoodsSuccessHandler);
            Dispatcher.addEventListener(EventName.RunBusinessRefreshTimeEnd, this.onRefreshTimeEndHandler);
            Dispatcher.addEventListener(EventName.RunBusinessRefreshTimeChange, this.onRefreshTimeChange);
            Dispatcher.addEventListener(EventName.RunBusinessSilverNoteChange, this.onSilverNoteChangeHandler);
            Dispatcher.addEventListener(EventName.RunBusinessIntroOpenOrClose, this.onIntroOpenOrCloseHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRunBusinessRefreshDt, this.onRunBusinessRefreshDtHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackRunBusinessPackChange, this.onRunBusinessPackChangeHandler);
            
        }

        private function onUseTaskSilverNoteHandler(event:DataEvent) : void
        {
            if (this.runBusinessSilverNoteModule.isHide)
            {
                this.runBusinessSilverNoteModule.show();
                NpcEffectRule.registDialogEffectWindow(this.runBusinessSilverNoteModule);
            }
            var _loc_2:* = cache.pack.runBusinessPackCache.getItemsByJudge();
            this.runBusinessSilverNoteModule.update(_loc_2);
            this.runBusinessSilverNoteModule.updateTime();
            
        }

        private function onRunBusinessTaskAcceptHandler(event:DataEvent) : void
        {
            
        }

        private function onRunBusinessCurrentNpcHandler(event:DataEvent) : void
        {
            var _loc_2:* = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupRunBusiness);
            if (_loc_2)
            {
                this._npcId = event.data as int;
                GameProxy.runBusinessProxy.getRunBuessinessNpc(this._npcId);
                this.runBusinessModule.show();
                this.runBusinessModule.updateTime();
                NpcEffectRule.registDialogEffectWindow(this.runBusinessModule);
            }
            else
            {
                MsgManager.addBroadCast("我跟你好像没有生意上的往来吧？");
            }
            
        }

        private function onGetGoodsSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SRunBusinessNpc;
            cache.runBusiness.runBusinessNpc = _loc_2;
            this.runBusinessModule.update(_loc_2);
            
        }

        private function onRunBusinessSellGoodsHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            GameProxy.runBusinessProxy.runBuessinessSellItem(this._npcId, _loc_2.uid, _loc_2.amount);
            
        }

        private function onRunBusinessBuyGoodsHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            GameProxy.runBusinessProxy.runBuessinessBuyItem(this._npcId, _loc_2.itemCode, _loc_2.amount);
            
        }

        private function onRunBusinessRefreshDtHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SRunBusinessRefreshDt;
            cache.runBusiness.nextRefreshDate = _loc_2.nextRefreshDt;
            
        }

        private function onRunBusinessPackChangeHandler(param1:Object) : void
        {
            var _loc_2:Array = null;
            if (this._runBusinessModule != null && !this._runBusinessModule.isHide)
            {
                this._runBusinessModule.update(cache.runBusiness.runBusinessNpc);
            }
            if (this._runBusinessSilverNoteModule != null && !this._runBusinessSilverNoteModule.isHide)
            {
                _loc_2 = cache.pack.runBusinessPackCache.getItemsByJudge();
                this._runBusinessSilverNoteModule.update(_loc_2);
            }
            
        }

        private function onRefreshTimeEndHandler(event:DataEvent) : void
        {
            if (this._runBusinessModule != null && !this._runBusinessModule.isHide)
            {
                GameProxy.runBusinessProxy.getRunBuessinessNpc(this._npcId);
            }
            
        }

        private function onRefreshTimeChange(event:DataEvent) : void
        {
            if (this._runBusinessModule != null && !this._runBusinessModule.isHide)
            {
                this._runBusinessModule.updateTime();
            }
            if (this._runBusinessSilverNoteModule != null && !this._runBusinessSilverNoteModule.isHide)
            {
                this._runBusinessSilverNoteModule.updateTime();
            }
            
        }

        private function onSilverNoteChangeHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (this._runBusinessModule != null)
            {
                this._runBusinessModule.setMoney(_loc_2);
            }
            if (this._runBusinessSilverNoteModule != null)
            {
                this._runBusinessSilverNoteModule.setMoney(_loc_2);
            }
            
        }

        private function onIntroOpenOrCloseHandler(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (this._introWindow == null)
            {
                this._introWindow = new RunBusinessIntroWindow();
            }
            if (!this.runBusinessModule.isHide)
            {
                _loc_2 = this.runBusinessModule.x + this.runBusinessModule.width;
                _loc_3 = this.runBusinessModule.y + this.runBusinessModule.height - this._introWindow.height;
            }
            else if (!this.runBusinessSilverNoteModule.isHide)
            {
                _loc_2 = this.runBusinessSilverNoteModule.x + this.runBusinessSilverNoteModule.width;
                _loc_3 = this.runBusinessSilverNoteModule.y + this.runBusinessSilverNoteModule.height - this._introWindow.height;
            }
            if (this._introWindow.isHide)
            {
                this._introWindow.show(_loc_2, _loc_3);
            }
            else
            {
                this._introWindow.hide();
            }
            
        }

    }
}
